package com.lanou3g.code0515.view;

import com.lanou3g.code0515.controller.Controller;
import com.lanou3g.code0515.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/15;
 * Otherwise, I don't know who created it either
 */

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Controller controller = new Controller();
        User user = controller.login(username,password);
        //根据user是否为空 来判断登录成功与否
        String msg = "";
        if (user == null){
            msg = "用户名/密码错误";
        } else {
            msg = "登录成功";
        }

        // 向request域中放值
        // 值以key-value的形式存放
        // key只能是String,value可以是任何对象
        request.setAttribute("msg",msg);

        request.getRequestDispatcher("/msg")
                .forward(request,response);
       // response.getWriter().println(msg);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
